package com.company.module.net.interceptor;//package com.company.app.common.http.interceptor;
//
//import android.content.Context;
//import android.util.Log;
//
//import java.io.IOException;
//
//import okhttp3.Interceptor;
//import okhttp3.Request;
//import okhttp3.Response;
///**
// * Created by drore on 17/5/17.
// */
//
//public class CheckCookiesInterceptor implements Interceptor {
//    @Override
//    public Response intercept(Chain chain) throws IOException {
//        Response originalResponse = chain.proceed(chain.request());
//        if (isExpired(originalResponse)) {
//
//            if (refreshCookies()) {
//                Log.e("CheckCookiesInterceptor", "401,if");
//                Request.Builder builder = chain.request().newBuilder();
//                String preferences = BaseApplication.getInstance()
//                        .getSharedPreferences("cookie", Context.MODE_PRIVATE)
//                        .getString("request-token", "null");
//                builder.addHeader("request-token", preferences);
//                return chain.proceed(builder.build());
//            } else {
//                Log.e("CheckCookiesInterceptor", "401,else");
////                Intent intent = new Intent();
////                intent.putExtra(Constant.SHOW_TOAST, true);
////                intent.setAction("com.drore.huanghk.login");
////                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
////                BaseApplication.getAppContext().getApplicationContext().startActivity(intent);
//                return originalResponse;
//            }
//        } else {
//            return originalResponse;
//        }
//
//    }
//
//    private boolean refreshCookies() {
//        if (!BaseApplication.getSpUtil().getBoolean(KeyUtils.IS_KEEP_LOGIN, true)) {
//            return false;
//        }
//        try {
//            String uname = BaseApplication.getSpUtil().getString(KeyUtils.USER_NAME);
//            String password = BaseApplication.getSpUtil().getString(KeyUtils.PASS_WORD);
//            retrofit2.Response<String> execute = HttpLogin.createHttpClient(uname, password).login().execute();
//            if (execute != null && execute.code() == 200) {
//                Log.e("refreshCookies", "refreshCookies success:" + execute.headers().get("request-token"));
//                BaseApplication.getInstance()
//                        .getSharedPreferences("cookie", Context.MODE_PRIVATE).edit().clear()
//                        .putString("request-token", execute.headers().get("request-token")).apply();
//                return true;
//            } else {
//                Log.e("refreshCookies", "refreshCookies success:" + execute.headers().get("request-token"));
//                return false;
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    private boolean isExpired(Response originalResponse) {
//        return originalResponse.code() == 401;
//    }
//}
